package com.demo.dao.mapper;

import com.demo.model.ShoppingCart;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class ShoppingCartMapper {
        public String insert(ShoppingCart shoppingCart) {
            return "insert into shopping_cart (productId, price, number,total, userId) values (#{productId},#{price},#{number},#{total},#{userId})";
        }

        public String delete(Integer id) {
            BEGIN();
            DELETE_FROM("shopping_cart");
            WHERE("id=#{id}");
            return SQL();
        }

        public String update(ShoppingCart shoppingCart) {
            BEGIN();
            UPDATE("shopping_cart");
            if (shoppingCart.getProductId() != null && shoppingCart.getProductId() > 0) {
                SET("productId=#{productId}");
            }
            if (shoppingCart.getNumber() != null && shoppingCart.getNumber() > 0) {
                SET("number=#{number}");
            }
            if (shoppingCart.getPrice() != null && shoppingCart.getPrice() > 0) {
                SET("price=#{price}");
            }
            if (shoppingCart.getTotal() != null && shoppingCart.getTotal() > 0) {
                SET("total=#{total}");
            }
            if (shoppingCart.getUserId() != null && shoppingCart.getUserId() > 0) {
                SET("userId=#{userId}");
            }
            WHERE("id=#{id}");
            String sql = SQL();
            System.out.println(sql);
            return sql;
        }

        public String select(Integer productId) {
            if (productId == null) {
                return "select * from shopping_cart order by id desc";
            } else {
                return "select * from shopping_cart where productId=#{productId} order by id desc";
            }
        }

        public String selectOne(Integer id) {
            return "select * from shopping_cart where id=#{id}";
        }

        public String selectOneByUserId(Integer userId) {
            return "select * from shopping_cart where userId=#{userId}";
        }

}
